package com.zgxl.common.es;

import cn.hutool.core.date.format.FastDateFormat;
import com.google.gson.JsonObject;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.rest.RestStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import java.util.Date;
import java.util.Map;
import java.util.Set;

@Component
public class ElasticSearchUtil {
    private Logger logger = LoggerFactory.getLogger(ElasticSearchUtil.class);

    @Autowired
    ElasticConnUtil elasticConnUtil;

    /**
     * 添加/更新数据进es缓存
     *
     * @param index  索引
     * @param type   索引类型
     * @param id     索引id
     * @param values 索引数据
     * @return true处理成功 fals 处理失败
     */
    public boolean addData2es(String index, String type, String id, Map<String, Object> values) {
        try {
            if (ObjectUtils.isEmpty(id) || ObjectUtils.isEmpty(values))
                return false;
            JsonObject jsonObject = new JsonObject();

            //添加版本号和时间戳
            jsonObject.addProperty("@version", "1");
            jsonObject.addProperty("@timestamp", FastDateFormat.getInstance("yyyy-MM-dd").format(new Date()) + "T" + FastDateFormat.getInstance("HH:mm:ss.SSS").format(new Date()) + "Z");

            Set<String> set = values.keySet();
            for (String key : set) {
                Object value = values.get(key);
                if (value == null)
                    continue;
                if (value instanceof Number) {
                    jsonObject.addProperty(key, (Number) value);
                } else if (value instanceof String) {
                    jsonObject.addProperty(key, (String) value);
                } else if (value instanceof Boolean) {
                    jsonObject.addProperty(key, (Boolean) value);
                } else if (value instanceof Character) {
                    jsonObject.addProperty(key, (Character) value);
                }
            }
            IndexResponse indexResponse = elasticConnUtil.getTransportClient().prepareIndex(index, type).setId(id).setSource(jsonObject.toString(), XContentType.JSON).get();
            System.out.println("addData2es>>>>>>>>index:" + index + ",type:" + type + ",id:" + id + "<>status:" + indexResponse.status().getStatus());
            if (indexResponse.status().getStatus() == RestStatus.ACCEPTED.getStatus() || indexResponse.status().getStatus() == RestStatus.CREATED.getStatus() || indexResponse.status().getStatus() == RestStatus.OK.getStatus()) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 从es缓存删除数据
     *
     * @param index 索引
     * @param type  索引类型
     * @param id    索引id
     * @return true处理成功 fals 处理失败
     */
    public boolean removeData4es(String index, String type, String id) {
        try {
            if (ObjectUtils.isEmpty(id))
                return false;
            DeleteResponse deleteResponse = elasticConnUtil.getTransportClient().prepareDelete(index, type, id).get();
            System.out.println("removeData4es>>>>>>>>index:" + index + ",type:" + type + ",id:" + id + "<>status:" + deleteResponse.status().getStatus());
            if (deleteResponse.status().getStatus() == RestStatus.ACCEPTED.getStatus() || deleteResponse.status().getStatus() == RestStatus.OK.getStatus()) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}
